import java.util.HashSet;
import java.util.Objects;

@SuppressWarnings("all")
public class _1Homework {
    public static void main(String[] args) {
        HashSet set = new HashSet();
        Person p1 = new Person(1001, "AA");
        Person p2 = new Person(1002, "BB");
        // 分别加入集合
        set.add(p1); // 是按照AA的hash值进行添加
        set.add(p2);

        p1.name = "CC";
        // 改变后 删除按照CC的hash进行删除 所以找不到原有的值
        System.out.println(set.remove(p1)); // false
        System.out.println(set);
        // 添加时比对的是原来AA+1001 的hash值 和1001 CC的hash不同
        set.add(new Person(1001, "CC"));
        System.out.println(set);

        // 两个去重
        set.add(new Person(1001, "AA"));
        System.out.println(set); // equals不同
        set.add(new Person(1001, "AA"));
        System.out.println(set);
    }
}


class Person {
    public String name;
    public int id;

    public Person(int id, String name) {
        this.name = name;
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Person person = (Person) o;
        return id == person.id &&
                Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, id);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }
}

